package com.stormrage.edu.ctrl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.stormrage.edu.serv.learn.LeaApplyServ;
import com.stormrage.edu.serv.learn.LeaTermServ;
import com.stormrage.entity.learn.rep.LeaTermRep;
import com.stormrage.entity.learn.req.LeaBunchReq;
import com.stormrage.entity.rep.ErrorRep;
import com.stormrage.entity.shiro.ShiroAcct;
import com.stormrage.shiro.bind.CurrentUser;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: LeaApplyCtrl.java
 * @Package com.stormrage.edu.ctrl
 * @Description: 报名
 * @author www.stormragetech.com
 * @date 2016年4月13日
 * @version V1.0
 */
@Controller
@RequestMapping("/apply")
public class LeaApplyCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(LeaApplyCtrl.class);

	@Value("#{constants['html.title']}")
	private String htmlTitle;

	@Value("#{constants['html.logo']}")
	private String htmlLogo;

	@Value("#{constants['path.root']}")
	private String rootPath;

	@Value("#{constants['url.root']}")
	private String rootUrl;

	@Value("#{constants['path.excel']}")
	private String excelPath;

	@Autowired
	private LeaApplyServ leaApplyServ;

	@Autowired
	private LeaTermServ leaTermServ;

	@RequestMapping(value = "/create/{id}", method = RequestMethod.GET)
	public String create(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug(" 【 Controller 】- 【 LeaApplyCtrl 】- 【 create 】-【 GET 】- 开始运行  ");

		try {

			// ------------------ 获取菜单 ------------------

			model.addAttribute("user", curr);
			model.addAttribute("htmltitle", htmlTitle);
			model.addAttribute("htmllogo", htmlLogo);

			model.addAttribute("id", id);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】- 【 LeaApplyCtrl 】- 【 create 】-【 GET 】- 结束运行  ");
		return "apply/create";

	}

	@RequestMapping(value = "/create/{id}", method = RequestMethod.POST)
	public String create(@CurrentUser ShiroAcct curr, @RequestParam(value = "id", required = true) Integer id,
			@RequestParam(value = "file", required = true) MultipartFile file, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】- 【 LeaApplyCtrl 】- 【 create 】-【 POST 】- 开始运行  ");

		logger.debug(" id 为 " + id);

		ErrorRep err = null;
		String errMsg = "";

		if (null == file) {
			errMsg = "提示信息：报名失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/bunch/apply";
		}

		try {

			// ------------------ Ctrl操作 ------------------

			String fileName = leaApplyServ.upload(file, rootPath + excelPath);

			LeaBunchReq bunch = LeaBunchReq.instance(id, Integer.valueOf(curr.getId()), fileName);

			logger.debug(" term id 为 " + bunch.getTermId());

			Integer index = leaApplyServ.apply(bunch);
			
			errMsg = "提示信息：报名成功，班级编号为【" + index + "】";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = "提示信息：报名失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/bunch/apply";
		}

		logger.debug(" 【 Controller 】-【 LeaApplyCtrl 】-【 Create 】-【 POST 】- 结束运行 ");
		return "redirect:/bunch/apply";

	}

	@RequestMapping(value = "/add/{id}", method = RequestMethod.GET)
	public String add(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug(" 【 Controller 】- 【 LeaApplyCtrl 】- 【 create 】-【 GET 】- 开始运行  ");

		try {

			// ------------------ 获取菜单 ------------------

			model.addAttribute("user", curr);
			model.addAttribute("htmltitle", htmlTitle);
			model.addAttribute("htmllogo", htmlLogo);

			model.addAttribute("id", id);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】- 【 LeaApplyCtrl 】- 【 create 】-【 GET 】- 结束运行  ");
		return "apply/create";

	}

	@RequestMapping(value = "/add/{id}", method = RequestMethod.POST)
	public String add(@CurrentUser ShiroAcct curr, @RequestParam(value = "id", required = true) Integer id,
			@RequestParam(value = "file", required = true) MultipartFile file, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】- 【 LeaApplyCtrl 】- 【 create 】-【 POST 】- 开始运行  ");

		logger.debug(" id 为 " + id);

		ErrorRep err = null;
		String errMsg = "";

		if (null == file) {
			errMsg = "提示信息：报名失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/bunch/apply";
		}

		try {

			// ------------------ Ctrl操作 ------------------

			String fileName = leaApplyServ.upload(file, rootPath + excelPath);

			LeaBunchReq bunch = LeaBunchReq.instance(id, Integer.valueOf(curr.getId()), fileName);

			logger.debug(" bunch id 为 " + bunch.getTermId());

			leaApplyServ.add(bunch);

			errMsg = "提示信息：报名成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = "提示信息：报名失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/bunch/apply";
		}

		logger.debug(" 【 Controller 】-【 LeaApplyCtrl 】-【 Create 】-【 POST 】- 结束运行 ");
		return "redirect:/bunch/apply";

	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(@CurrentUser ShiroAcct curr, Model model) {

		logger.debug(" 【 Controller 】- 【 LeaApplyCtrl 】- 【 create 】-【 GET 】- 开始运行  ");

		try {

			// ------------------ Ctrl 操作 ------------------
			List<LeaTermRep> array = leaTermServ.queryList();

			model.addAttribute("user", curr);
			model.addAttribute("htmltitle", htmlTitle);
			model.addAttribute("htmllogo", htmlLogo);

			model.addAttribute("data", array);
			model.addAttribute("path", rootUrl + excelPath);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】- 【 LeaApplyCtrl 】- 【 create 】-【 GET 】- 结束运行  ");
		return "apply/list";

	}

}
